<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login21.php');
    exit;
}

require_once 'teaching21.php';

$error = '';
$success = '';

$teachingModel = new Teaching();
$teachers = $teachingModel->getTeachers();
$courses = $teachingModel->getCourses();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $teacher_id = $_POST['teacher_id'] ?? '';
    $course_id = $_POST['course_id'] ?? '';
    $semester = $_POST['semester'] ?? '';
    
    if (empty($teacher_id)) {
        $error = '请选择教师';
    } elseif (empty($course_id)) {
        $error = '请选择课程';
    } elseif (empty($semester)) {
        $error = '请输入学期';
    } else {
        try {
            // 检查是否已存在相同的授课安排
            if ($teachingModel->checkTeachingExists($teacher_id, $course_id, $semester)) {
                $error = '该授课安排已存在，请勿重复添加';
            } else {
                $teachingModel->createTeaching($teacher_id, $course_id, $semester);
                header("Location: teachings21.php?success=授课安排添加成功");
                exit;
            }
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 添加授课安排</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>教学服务系统</h1>
        </div>
    </div>

    <div class="main-nav">
        <div class="container">
            <ul>
                <li><a href="index.php">主页</a></li>
                <li><a href="departments21.php">系部管理</a></li>
                <li><a href="classes21.php">班级管理</a></li>
                <li><a href="students21.php">学生管理</a></li>
                <li><a href="teachers21.php">教师管理</a></li>
                <li><a href="courses21.php">课程管理</a></li>
                <li><a href="scores21.php">成绩管理</a></li>
                <li><a href="teachings21.php">授课管理</a></li>
                <li class="logout"><a href="logout21.php">退出登录</a></li>
            </ul>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">添加授课安排</h2>
            </div>
            
            <?php if ($error): ?>
            <div class="message message-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <div class="card-body">
                <form method="post">
                    <div class="form-group">
                        <label for="teacher_id">教师</label>
                        <select id="teacher_id" name="teacher_id" required class="form-control">
                            <option value="">请选择教师</option>
                            <?php foreach ($teachers as $teacher): ?>
                            <option value="<?php echo $teacher['teacher_id']; ?>">
                                <?php echo htmlspecialchars($teacher['teacher_name']); ?> (<?php echo htmlspecialchars($teacher['department_name']); ?>)
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="course_id">课程</label>
                        <select id="course_id" name="course_id" required class="form-control">
                            <option value="">请选择课程</option>
                            <?php foreach ($courses as $course): ?>
                            <option value="<?php echo $course['course_id']; ?>">
                                <?php echo htmlspecialchars($course['course_name']); ?> (<?php echo $course['credit']; ?>学分)
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="semester">学期</label>
                        <input type="text" id="semester" name="semester" required
                               class="form-control" placeholder="例: 2019-2020-1" maxlength="50">
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a href="teachings21.php" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>